package com.ias.train.controller;

import com.ias.train.common.JsonResult;
import com.ias.train.entity.UserInfo;
import com.ias.train.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: train
 * @description: 用户控制类
 * @author: Daniel Shen
 * @create: 2022-04-23 15:20
 */
@Api("用户控制类")
@RestController
@RequestMapping("/ias/user")
public class UserController {
    @Resource
    UserService userService;

    /**
     * 新增用户
     * @param userInfo 用户实体数据
     * @return 新增后的用户数据
     * @exsample: (POST)http://localhost:9081/ias/user/add
     * {
     *     "userUid": "zhangsan",
     *     "userName": "张三",
     *     "userPasswd": "123456",
     *     "userLocked": false,
     *     "userType": "private",
     *     "userEmail": "ias@beyondbit.com"
     * }
     */
    @ApiOperation("添加用户数据")
    @PostMapping("/add")
    public JsonResult add(@RequestBody UserInfo userInfo) {
        return JsonResult.success(userService.add(userInfo), "添加用户成功！");
    }

    /**
     * 编辑用户
     * @param userInfo 用户实体数据
     * @return 编辑后的新用户数据
     * @exsample: (POST)http://localhost:9081/ias/user/edit
     * {
     *     "userId": "0ddc4721-aa50-41b8-b5cc-90f12a690d8e",
     *     "userUid": "zhangsan",
     *     "userName": "张三-1",
     *     "userPasswd": "123456",
     *     "userLocked": false,
     *     "userType": "private",
     *     "userEmail": "ias-1@beyondbit.com"
     * }
     */
    @ApiOperation("编辑用户数据")
    @PostMapping("/edit")
    public JsonResult edit(@RequestBody UserInfo userInfo) {
        return JsonResult.success(userService.edit(userInfo),"编辑用户成功！");
    }

    /**
     * 删除用户
     * @param userInfo 用户实体数据
     * @return 删除的用户数据
     * @exsample: (POST)http://localhost:9081/ias/user/delete
     * {
     *     "userId": "7240bd36-fa89-4c8d-8424-c3555afb5659"
     * }
     */
    @ApiOperation("删除用户数据")
    @PostMapping("/delete")
    public JsonResult delete(@RequestBody UserInfo userInfo) {
        return JsonResult.success(userService.delete(userInfo), "删除用户成功！");
    }

    /**
     * 获取用户信息
     * @param userInfo 用户实体数据，目前仅支持根据 userId 获取
     * @return 用户信息
     * @exsample: (POST)http://localhost:9081/ias/user/getuser
     * {
     *     "userId": "b7471ae1-3724-4652-970b-719effeb408b"
     * }
     */
    @ApiOperation("获取单个用户数据")
    @PostMapping("getuser")
    public JsonResult getUser(@RequestBody UserInfo userInfo) {
        return JsonResult.success(userService.getOneUser(userInfo), "获取用户信息成功！");
    }

    /**
     * 获取用户集合数据
     * @param userInfo  用户实体数据，目前只支持userId, userUid, userLocked, userType 单个条件或多个条件（and）组合查询
     * @return 用户集合
     * @exsample: (POST)http://localhost:9081/ias/user/getusers
     * {
     *     "userLocked": false,
     *     "userType": "private"
     * }
     */
    @ApiOperation("获取用户集合数据")
    @PostMapping("/getusers")
    public JsonResult getUsers(@RequestBody UserInfo userInfo) {
        return JsonResult.success(userService.getUsers(userInfo), "获取用户集合成功！");
    }
}
